<?php
header('Content-type: text/html; charset=UTF-8');
class login extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('usermodel', 'user', TRUE);
	}
	function index(){
		if ($this->session->userdata('LoggedIn')){
			redirect('home');
		}else{
			$this->show_login(false);
		}
	}
	
	
	function show_login($show_error = ''){
		$this->data['error'] = $show_error;
		
		$this->load->helper('form');
		$this->load->view('login', $this->data);
	}
	
	function login_user(){
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		
		if($username && $password && $this->user->validate_user($username, $password)){
			redirect('home');
		}else{
			 $this->show_login(true);
		}
	}
	
	function logout_user(){
		$this->session->sess_destroy();
		$this->index();
	}
}